# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=miniupnpc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2.3.2
pkgrel=1
pkgdesc="A small UPnP client library/tool to access Internet Gateway Devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://miniupnp.free.fr"
msys2_repository_url="https://github.com/miniupnp/miniupnp"
msys2_references=(
  "cpe: cpe:2.3:a:miniupnp_project:miniupnpc"
)
license=('spdx:BSD-3-Clause')
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=(https://miniupnp.tuxfamily.org/files/${_realname}-${pkgver}.tar.gz{,.sig}
        002-python-module-build.patch)
sha256sums=('985de16d2e5449c3ba0d3663a0c76cb2bff82472a0eb7a306107d93f44586ffe'
            'SKIP'
            '79857b7808ece306cfae0bfb6c072a0022aaba1d8624273d51ed4e4fc741509c')
validpgpkeys=('751E9FF6944A3B36A5432216DB511043A31ACAAF') # miniupnp <miniupnp@free.fr>

prepare() {
  cd ${_realname}-${pkgver}

  patch -p1 -i "${srcdir}"/002-python-module-build.patch
}

build() {
  cp -rf ${_realname}-${pkgver} build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUPNPC_BUILD_SAMPLE=OFF \
    -DUPNPC_BUILD_SHARED=ON \
    -DUPNPC_BUILD_STATIC=ON \
    -DUPNPC_BUILD_TESTS=OFF \
    .

  ${MINGW_PREFIX}/bin/cmake --build .

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setupmingw32.py build
}

package_miniupnpc() {
  cd build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_python-miniupnpc() {
  pkgdesc='Python bindings for miniupnpc (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-python")

  cd build-${MSYSTEM}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    "${MINGW_PREFIX}"/bin/python setupmingw32.py install --prefix="${MINGW_PREFIX}" \
      --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
